package com.ruoyi.system.service.impl.biz;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.biz.BmMember;
import com.ruoyi.system.domain.biz.BmMemberReport;
import com.ruoyi.system.mapper.biz.BmMemberAmountRecordMapper;
import com.ruoyi.system.mapper.biz.BmMemberMapper;
import com.ruoyi.system.mapper.biz.BmMemberReportMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.biz.BmAmountDetailRecordMapper;
import com.ruoyi.system.domain.biz.BmAmountDetailRecord;
import com.ruoyi.system.service.biz.IBmAmountDetailRecordService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 会员消费明细Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-11-18
 */
@Service
public class BmAmountDetailRecordServiceImpl implements IBmAmountDetailRecordService 
{
    @Autowired
    private BmAmountDetailRecordMapper bmAmountDetailRecordMapper;
    @Autowired
    private BmMemberReportMapper bmMemberReportMapper;
    @Autowired
    private BmMemberMapper bmMemberMapper;

    @Autowired
    private BmMemberAmountRecordMapper bmMemberAmountRecordMapper;

    /**
     * 查询会员消费明细
     * 
     * @param id 会员消费明细主键
     * @return 会员消费明细
     */
    @Override
    public BmAmountDetailRecord selectBmAmountDetailRecordById(Long id)
    {
        return bmAmountDetailRecordMapper.selectBmAmountDetailRecordById(id);
    }

    /**
     * 查询会员消费明细列表
     * 
     * @param bmAmountDetailRecord 会员消费明细
     * @return 会员消费明细
     */
    @Override
    public List<BmAmountDetailRecord> selectBmAmountDetailRecordList(BmAmountDetailRecord bmAmountDetailRecord)
    {
        return bmAmountDetailRecordMapper.selectBmAmountDetailRecordList(bmAmountDetailRecord);
    }

    /**
     * 新增会员消费明细
     * 
     * @param bmAmountDetailRecord 会员消费明细
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBmAmountDetailRecord(BmAmountDetailRecord bmAmountDetailRecord)
    {
        bmAmountDetailRecord.setCreateTime(DateUtils.getNowDate());
        //汇总表增加打球次数，最后消费时间，并且扣除余额


        //处理统计数据
        BmMember bmMember =bmMemberMapper.selectBmMemberByUserAccount(bmAmountDetailRecord.getAccount());
        if(bmMember.getSex().equals("1")){
            if(bmMember.getVip().equals("N")){
                bmAmountDetailRecord.setCosted(30l);
            }else {
                bmAmountDetailRecord.setCosted(bmAmountDetailRecord.getWcosted());
            }
        }else{
            if(bmMember.getVip().equals("0")){
                if(bmMember.getVip().equals("N")) {
                    bmAmountDetailRecord.setCosted(40l);
                }
            }else {
                bmAmountDetailRecord.setCosted(bmAmountDetailRecord.getMcosted());
            }
        }
        BmMemberReport bmMemberReport= bmMemberReportMapper.selectBmMemberReportByUserAccount(bmAmountDetailRecord.getAccount());
        if(null == bmMemberReport){
            bmMemberReport= new BmMemberReport();
            Integer money = bmMemberAmountRecordMapper.totalAmountRecordByMember(bmAmountDetailRecord.getAccount());
            if(bmMember.getVip().equals("N")) {
                bmMemberReport.setSurplus(0l);
            }else {
                bmMemberReport.setSurplus(money==null?0l:money.longValue()-bmAmountDetailRecord.getCosted());
            }
            bmMemberReport.setCosted(bmAmountDetailRecord.getCosted());
            bmMemberReport.setUserAccount(bmAmountDetailRecord.getAccount());
            bmMemberReport.setLastPlayAmount(bmAmountDetailRecord.getCosted());
            bmMemberReport.setLastPlayTime(bmAmountDetailRecord.getCreateTime());
            bmMemberReport.setTotalNum(1l);
            bmMemberReportMapper.insertBmMemberReport(bmMemberReport);
        }else{
            if(bmMemberReport.getSurplus()!=0){
                bmMemberReport.setSurplus(bmMemberReport.getSurplus()-bmAmountDetailRecord.getCosted());
            }
            bmMemberReport.setLastPlayAmount(bmAmountDetailRecord.getCosted());
            bmMemberReport.setLastPlayTime(bmAmountDetailRecord.getCreateTime());
            bmMemberReport.setTotalNum(bmMemberReport.getTotalNum()+1);
            bmMemberReport.setCosted(bmMemberReport.getCosted()+ bmAmountDetailRecord.getCosted());
            bmMemberReportMapper.updateBmMemberReport(bmMemberReport);
        }
        return bmAmountDetailRecordMapper.insertBmAmountDetailRecord(bmAmountDetailRecord);
    }

    /**
     * 修改会员消费明细
     * 
     * @param bmAmountDetailRecord 会员消费明细
     * @return 结果
     */
    @Override
    public int updateBmAmountDetailRecord(BmAmountDetailRecord bmAmountDetailRecord)
    {
        bmAmountDetailRecord.setUpdateTime(DateUtils.getNowDate());
        return bmAmountDetailRecordMapper.updateBmAmountDetailRecord(bmAmountDetailRecord);
    }

    /**
     * 批量删除会员消费明细
     * 
     * @param ids 需要删除的会员消费明细主键
     * @return 结果
     */
    @Override
    public int deleteBmAmountDetailRecordByIds(String ids)
    {
        return bmAmountDetailRecordMapper.deleteBmAmountDetailRecordByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除会员消费明细信息
     * 
     * @param id 会员消费明细主键
     * @return 结果
     */
    @Override
    public int deleteBmAmountDetailRecordById(Long id)
    {
        return bmAmountDetailRecordMapper.deleteBmAmountDetailRecordById(id);
    }

    @Override
    public int totalSurplus() {
        return bmAmountDetailRecordMapper.totalSurplus();
    }

    @Override
    public int totalActiveNum() {
        return bmAmountDetailRecordMapper.totalActiveNum();
    }

    @Override
    public int totalCosted() {
        return bmAmountDetailRecordMapper.totalCosted();
    }

    @Override
    public int sumPlayNum() {
        return bmAmountDetailRecordMapper.sumPlayNum();
    }
}
